﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DBSchemaViewer.FrontEnd.DiagramUserControls;
using DBSchemaViewer.BackEnd.Entities;

namespace DBSchemaViewer.FrontEnd.Main.Helpers
{
    public class RelationshipDraggingHelper
    {
        /// <summary>
        /// Calculate the X position of the origin relation ship (depending the way you drag it could be on the 
        /// left or right side of the origin table owner.
        /// </summary>
        /// <param name="origin"></param>
        /// <param name="destination"></param>
        /// <param name="originTableWidth"></param>
        /// <param name="destinationTable"></param>
        /// <returns></returns>
        public double RelationShipControlOriginPositionX(Point origin, Point destination, double originTableWidth, double destinationTable)
        {
            double offsetX = 0;

            if (origin.X < (destination.X + destinationTable))
            {
                offsetX += originTableWidth;
            }

            return offsetX;
        }

        public double RelationShipControlDestinationPositionX(Point origin, Point destination, double originTableWidth, double destinationTableWidth)
        {
            double offsetX = 0;

            if ((origin.X + originTableWidth) > (destination.X))
            {
                offsetX += destinationTableWidth;
            }

            return offsetX;
        }

        /// <summary>
        /// Normally, the direction in which the fork (end connector terminator) is shown is automatically controlled
        /// by the connector componente, BUT there is a case where the left origin point property of the origin table 
        /// is just in between the destination table, in that case we should swo as well the connector twisted.
        /// </summary>
        /// <param name="OriginX"></param>
        /// <param name="destinationTableLeftProperty"></param>
        /// <returns></returns>
        public bool RelationShipControlShowTwistedFork(double OriginX, double destinationTableLeftProperty)
        {
            return (OriginX > destinationTableLeftProperty);
        }


        /// <summary>
        /// For the begin connector work the other way around, if it's in between, do not show the connector inverted
        /// (is still on the left side).
        /// </summary>
        /// <param name="OriginX"></param>
        /// <param name="destinationTableLeftProperty"></param>
        /// <param name="destinationTableWidth"></param>
        /// <returns></returns>
        public bool RelationShipControlDoNotShowTwistedBeginConnector(double OriginX, double destinationTableLeftProperty, double destinationTableWidth)
        {
            return (OriginX < (destinationTableLeftProperty + destinationTableWidth));
        }

        
        

        /// <summary>
        /// Just when dragging and Dropping, show 
        /// </summary>
        /// <param name="controlListHelper"></param>
        /// <param name="RelationshipControl"></param>
        public void SetPointOriginAndDestinationFromTemporaryDragRelationship(DynamicControlsListHelper controlListHelper, CtlDBRelationship RelationshipControl, Point destinationPoint)
        {
            // Let's calculate the position of the beggining and end point of the connector
            // based on the Table controls and fields
            Point origin = new Point();
            Point destination = destinationPoint;

            EntityRelation relation = RelationshipControl.EntityRelation_info;
            CtlDBTable originTable = controlListHelper.GetTableControlByID(relation.TableOriginID);

            // Setup Origin
            origin.Y = (double)originTable.GetValue(Canvas.TopProperty);
            origin.Y += originTable.GetFieldYOffset(relation.RelationFields[0].FieldOrigin);

            origin.X = (double)originTable.GetValue(Canvas.LeftProperty);
            double originOffsetX = RelationShipControlOriginPositionX(origin, destination, originTable.getTableWidth(), 0);
            origin.X += originOffsetX;

            // Destination just the mouse coordinate provided (dragging with the mouse temp relation)
            
            // Setup the previously calculated values
            //currRelationship.Origin = origin;
            //currRelationship.Destination = destination;
            //bool doNotShowTwistedBeginConnector = RelationShipControlDoNotShowTwistedBeginConnector((double)originTable.GetValue(Canvas.LeftProperty), (double)destinationTable.GetValue(Canvas.LeftProperty), destinationTable.getTableWidth());
            //bool showTwistedFork = RelationShipControlShowTwistedFork(origin.X, (double)destinationTable.GetValue(Canvas.LeftProperty));
            bool doNotShowTwistedBeginConnector = (origin.X - destination.X > 0);
            bool showTwistedFork = (origin.X - destination.X > 0);

            RelationshipControl.SetOriginAndDestination(origin, destination, doNotShowTwistedBeginConnector, showTwistedFork);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="controlListHelper"></param>
        /// <param name="RelationshipControl"></param>
        /// <param name="destination"></param>
       /* public void SetPointOriginAndDestinationFromTemporaryDragRelationship(DynamicControlsListHelper controlListHelper, CtlDBRelationship RelationshipControl, Point destination)
        {

        }*/

        /// <summary>
        /// Relations origina and destination coordinates are calculated on the fly
        /// based on the table origin / destination and field origin / destination
        /// </summary>
        /// <param name="controlListHelper"></param>
        /// <param name="RelationshipControl">Quite important the relationEntity_info must be informed</param>
        public void SetPointOriginAndDestinationFromGivenRelationshipEntity(DynamicControlsListHelper controlListHelper, CtlDBRelationship RelationshipControl)
        {
            // Let's calculate the position of the beggining and end point of the connector
            // based on the Table controls and fields
            Point origin = new Point();
            Point destination = new Point();

            EntityRelation relation = RelationshipControl.EntityRelation_info;

            CtlDBTable originTable = controlListHelper.GetTableControlByID(relation.TableOriginID);
            CtlDBTable destinationTable = controlListHelper.GetTableControlByID(relation.TableDestinationID);
            
            // Setup Origin
            origin.X = (double)originTable.GetValue(Canvas.LeftProperty);
            

            origin.Y = (double)originTable.GetValue(Canvas.TopProperty);
            origin.Y += originTable.GetFieldYOffset(relation.RelationFields[0].FieldOrigin);

            // Setup Destination
            destination.X = (double)destinationTable.GetValue(Canvas.LeftProperty);
            

            //origin.X += originOffsetX;
            //destination.X += destinationOffsetX;

            destination.Y = (double)destinationTable.GetValue(Canvas.TopProperty);
            destination.Y += destinationTable.GetFieldYOffset(relation.RelationFields[0].FieldDestination);

            // ** Dragging helpers
            double originOffsetX = RelationShipControlOriginPositionX(origin, destination, originTable.getTableWidth(), destinationTable.getTableWidth());
            double destinationOffsetX = RelationShipControlDestinationPositionX(origin, destination, originTable.getTableWidth(), destinationTable.getTableWidth());
            // ** /Dragging helpers

            // ***
            origin.X += originOffsetX;
            destination.X += destinationOffsetX;
            // ***

            // Setup the previously calculated values
            //currRelationship.Origin = origin;
            //currRelationship.Destination = destination;
            bool doNotShowTwistedBeginConnector = RelationShipControlDoNotShowTwistedBeginConnector((double)originTable.GetValue(Canvas.LeftProperty), (double)destinationTable.GetValue(Canvas.LeftProperty), destinationTable.getTableWidth());
            bool showTwistedFork = RelationShipControlShowTwistedFork(origin.X, (double)destinationTable.GetValue(Canvas.LeftProperty));

            RelationshipControl.SetOriginAndDestination(origin, destination, doNotShowTwistedBeginConnector, showTwistedFork);
        }
    }
}
